package com.housing.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.zmetaport.common.core.utils.MapstructUtils;
import com.zmetaport.common.core.utils.StringUtils;
import com.zmetaport.common.mybatis.core.page.TableDataInfo;
import com.zmetaport.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.housing.domain.bo.DictDataBo;
import com.housing.domain.vo.DictDataVo;
import com.housing.domain.DictData;
import com.housing.mapper.DictDataMapper;
import com.housing.service.IDictDataService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 字典数据Service业务层处理
 *
 * @author chensm
 * @date 2024-12-15
 */
@RequiredArgsConstructor
@Service
public class DictDataServiceImpl implements IDictDataService {

    private final DictDataMapper baseMapper;

    /**
     * 查询字典数据
     */
    @Override
    public DictDataVo queryById(Long dictCode){
        return baseMapper.selectVoById(dictCode);
    }

    /**
     * 查询字典数据列表
     */
    @Override
    public TableDataInfo<DictDataVo> queryPageList(DictDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DictData> lqw = buildQueryWrapper(bo);
        Page<DictDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询字典数据列表
     */
    @Override
    public List<DictDataVo> queryList(DictDataBo bo) {
        LambdaQueryWrapper<DictData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DictData> buildQueryWrapper(DictDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DictData> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDictSort() != null, DictData::getDictSort, bo.getDictSort());
        lqw.eq(StringUtils.isNotBlank(bo.getDictLabel()), DictData::getDictLabel, bo.getDictLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getDictValue()), DictData::getDictValue, bo.getDictValue());
        lqw.eq(StringUtils.isNotBlank(bo.getDictType()), DictData::getDictType, bo.getDictType());
        lqw.eq(StringUtils.isNotBlank(bo.getCssClass()), DictData::getCssClass, bo.getCssClass());
        lqw.eq(StringUtils.isNotBlank(bo.getListClass()), DictData::getListClass, bo.getListClass());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDefault()), DictData::getIsDefault, bo.getIsDefault());
        return lqw;
    }

    /**
     * 新增字典数据
     */
    @Override
    public Boolean insertByBo(DictDataBo bo) {
        DictData add = MapstructUtils.convert(bo, DictData.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDictCode(add.getDictCode());
        }
        return flag;
    }

    @Override
    public Boolean insertByBos(List<DictDataBo> bos) {
        List<DictData> adds = MapstructUtils.convert(bos, DictData.class);
        if(CollUtil.isEmpty(adds)){
            return false;
        }
        baseMapper.insertBatch(adds);
        return true;
    }

    /**
     * 修改字典数据
     */
    @Override
    public Boolean updateByBo(DictDataBo bo) {
        DictData update = MapstructUtils.convert(bo, DictData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DictData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除字典数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
